#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <time.h>
#include "proto.h"

int main()
{
	struct studentMsg sendbuf;
	key_t key;
	int msgid;
	int num = 5;

	key = ftok(FTOKNAME, PROJID);
	if (key < 0)
	{
		perror("ftok()");
		exit(1);
	}

	msgid = msgget(key, 0);
	if (msgid < 0)
	{
		perror("msgget()");
		exit(1);
	}
	
	srand((unsigned int)time(NULL));
	while (num--)
	{
		sprintf(sendbuf.name, "std%d", num);
		sendbuf.age = rand() % 30;
		sendbuf.math = rand() % 100;
		sendbuf.English = rand() % 100;

		if (msgsnd(msgid, &sendbuf, sizeof(sendbuf) - sizeof(long), 0) < 0)
		{
			perror("msgrcv()");
			exit(1);
		}
	}
	
	printf("send over!\n");
	exit(0);
}

